package com.sy.simpleegg.room.dao

import androidx.room.Dao
import androidx.room.Delete
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import androidx.room.Transaction
import androidx.room.Update
import com.sy.simpleegg.ext.logFile
import com.sy.simpleegg.room.entity.CoinEntity

@Dao
interface CoinDao {
    @Query("SELECT * FROM tb_coin")
    fun getAll(): MutableList<CoinEntity>?

    @Query("SELECT * FROM tb_coin ORDER BY id DESC LIMIT 1")
    fun getLastCoins(): MutableList<CoinEntity>?

    @Update
    fun update(coinEntity: CoinEntity)

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    fun insert(coinEntity: CoinEntity)

    @Transaction
    fun insertTransation(coinEntity: CoinEntity) {
        try {
            val lastCoins = getLastCoins()
            if (lastCoins.isNullOrEmpty()) {
                when (coinEntity.type) {
                    //出币
                    0 -> {
                        coinEntity.remainCoin = 0 - coinEntity.coins
                        coinEntity.outCoinTotal = coinEntity.coins
                    }
                    //补币
                    1 -> {
                        coinEntity.remainCoin = coinEntity.coins
                        coinEntity.buCoinTotal = coinEntity.coins
                    }
                    //清币
                    2 -> {
                        coinEntity.remainCoin = 0 - coinEntity.coins
                        coinEntity.clearCoinTotal = coinEntity.coins
                    }
                }
                insert(coinEntity)
            } else {
                val lastCoin = lastCoins[0]
                when (coinEntity.type) {
                    //出币
                    0 -> {
                        coinEntity.remainCoin = lastCoin.remainCoin - coinEntity.coins
                        coinEntity.outCoinTotal = lastCoin.outCoinTotal + coinEntity.coins
                    }
                    //补币
                    1 -> {
                        coinEntity.remainCoin = lastCoin.remainCoin + coinEntity.coins
                        coinEntity.buCoinTotal = lastCoin.buCoinTotal + coinEntity.coins
                    }
                    //清币
                    2 -> {
                        coinEntity.remainCoin = lastCoin.remainCoin - coinEntity.coins
                        coinEntity.clearCoinTotal = lastCoin.clearCoinTotal + coinEntity.coins
                    }
                }
                insert(coinEntity)
            }
        }catch (e: Exception){
            e.printStackTrace()
            logFile("插入游戏币数据异常：${e.message}")
        }
    }

    @Transaction
    fun insertAll(coinEntities: MutableList<CoinEntity>) {
        coinEntities.forEach {
            insert(it)
        }
    }

//    @Query("SELECT * FROM tb_coin WHERE type = 3 AND date >= DATE('now','-30 day')")
    @Query("SELECT * FROM tb_coin WHERE date >= :startDate AND date <= :endDate ORDER BY date DESC")
    fun getClearCoins(startDate: String, endDate: String): MutableList<CoinEntity>?

    @Query("SELECT * FROM tb_coin WHERE date = :date ORDER BY id DESC")
    fun getByDate(date: String): MutableList<CoinEntity>?

    @Delete
    fun delete(coinEntity: CoinEntity)

    @Query("DELETE FROM tb_coin")
    fun deleteAll()
}